import axios from 'axios';
import { removeToken, getToken } from '@/untils/auth';
import { message } from 'antd';
const httpInstance = axios.create({
    baseURL: import.meta.env.VITE_APP_BASE_API,
    timeout: 10000
})
// 请求拦截器
httpInstance.interceptors.request.use(config => {
    if (getToken()) {
        config.headers['Authorization'] = 'Bearer ' + getToken()

    }
    return config
}, error => {
    return Promise.reject(error)
})
// 响应拦截器
httpInstance.interceptors.response.use(response => {
    let res_data = response.data
    switch (res_data.code) {
        case 401:
            message.error('认证失败，请重新登录！');
            removeToken();
            break;
        case 500:
            message.error(res_data.msg);
            break;
        default:
            break;
    }
    return response

}, error => {
    message.error('网络请求错误，请检查网络连接！');
    return Promise.reject(error)
})

export default httpInstance;